package com.LeeCode;

/**
 * 交换链表中的节点
 */

public class Code1721 {
    public static void main(String[] args) {
        int[] arr = {1};
        int k = 1;
        ListNode head = Utils.arrayToListNode(arr);
        Utils.printListNode(new Code1721().swapNodes(head, k));
    }

    public ListNode swapNodes(ListNode head, int k) {
        ListNode dummy = new ListNode(0, head);
        ListNode last = dummy, p1 = dummy;
        for (int i = 1; i <= k; i++) {
            last = last.next;
        }
        ListNode p0 = last;
        while (last != null) {
            p1 = p1.next;
            last = last.next;
        }

        int temp = p0.val;
        p0.val = p1.val;
        p1.val = temp;

        return dummy.next;
    }
}
